package com.copa.listener;

import com.copa.entity.Music;
import com.copa.service.CommandService;
import com.copa.service.MusicFactory;
import com.copa.service.MusicService;
import com.copa.utils.MyCatCodeUtil;
import love.forte.simbot.annotation.*;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

import static com.copa.constant.Constants.GROUP_COMMAND;
import static com.copa.constant.Constants.PRIVATE_COMMAND;


/**
 * @author copa
 * @createDate 2022-02-16 21:45
 * @function 私聊处理
 */
@Component
public class PrivateListener {

    @Autowired
    private CommandService commandService;

    @OnPrivate
    @Filters(
            value = {
                    @Filter(value = "#音乐", matchType = MatchType.STARTS_WITH, trim = true)
            }
    )
    public void excludeCommand(PrivateMsg msg, Sender sender) {
        String pattern = "^#音乐：.+";
        if (msg.getMsg().length() == 3
                || !Pattern.matches(pattern, msg.getMsg())) {
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    commandService.findCommandByCommandAndType("error", PRIVATE_COMMAND)
                            + msg.getAccountInfo().getAccountNickname() + "~");
        }
    }

    @OnPrivate
    public void tempUserAndOtherMessage(PrivateMsg msg, Sender sender) {
        if (!PrivateMsg.Type.FRIEND.equals(msg.getPrivateMsgType())
                && !msg.getMsgContent().getCats().get(0).getType().contains("nudge")) {
            // 非好友处理
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    commandService.findCommandByCommandAndType("unFriend", PRIVATE_COMMAND));
            return;
        }
        if (msg.getMsgContent().getCats().get(0).getType().contains("poke")) {
            // 窗口抖动
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    commandService.findCommandByCommandAndType("poke", PRIVATE_COMMAND)
                            + msg.getAccountInfo().getAccountNickname() + "~");
            return;
        }
        if (msg.getMsgContent().getCats().get(0).getType().contains("nudge")) {
            // 头像抖动
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    commandService.findCommandByCommandAndType("nudge", PRIVATE_COMMAND)
                            + msg.getAccountInfo().getAccountNickname() + "~");
            return;
        }
        if (!commandService.findCountByCommandAndType(msg.getMsgContent().getMsg(), PRIVATE_COMMAND)) {
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    commandService.findCommandByCommandAndType("error", PRIVATE_COMMAND)
                            + msg.getAccountInfo().getAccountNickname() + "~");
        }
    }

    @OnPrivate
    @Filter(value = "#骰子", matchType = MatchType.STARTS_WITH, trim = true)
    public void privateDiceMessage(PrivateMsg msg, Sender sender) {
        if (PrivateMsg.Type.FRIEND.equals(msg.getPrivateMsgType())) {
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    MyCatCodeUtil.getDice());
        }
    }

    @OnPrivate
    @Filter(value = "#音乐：{{content}}", matchType = MatchType.REGEX_MATCHES, trim = true)
    public void privateMusicMessage(PrivateMsg msg, Sender sender,
                             @FilterValue(value = "content") String content) {
        if (PrivateMsg.Type.FRIEND.equals(msg.getPrivateMsgType())) {
            if (!content.contains("#")) {
                sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                        commandService.findCommandByCommandAndType("#error音乐指令", PRIVATE_COMMAND));
                return;
            }
            String[] contentSplit = content.split("#");
            MusicService musicService = MusicFactory.getMusicService(contentSplit[0]);
            // 是否需要下载歌曲
            boolean needLink = contentSplit.length > 2 && "下载".equals(contentSplit[2]);
            Music music = musicService.searchMusicSync(contentSplit[1], needLink);
            sender.sendPrivateMsg(msg.getAccountInfo().getAccountCode(),
                    MyCatCodeUtil.getMusicStr(music));
        }
    }

    /**
     * 窗口抖动
     * @param
     * @return
     */
//    @OnPrivate
//    public ReplyAble privatePoke(PrivateMsg msg) {
//        if (msg.getMsgContent().getCats().get(0).getType().contains("poke")) {
//            return Reply.reply("别闹啦" + msg.getAccountInfo().getAccountNickname() + "~", true);
//        }
//        return null;
//    }
}
